/*
 * Created on Mar 10, 2004
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package org.vectrics.user;

import java.util.Comparator;
import java.util.Date;

import org.apache.log4j.Logger;


/**
 * @author mmoore
 *
 * Helps sort interviews by date.
 */
public class PersonEncounterDateComparator implements Comparator {
	private Logger log = Logger.getLogger(this.getClass());

	public PersonEncounterDateComparator() {
		super();
	}

	public int compare(Object arg0, Object arg1) {
		PersonEncounter encounter1 = (PersonEncounter)arg1;
		PersonEncounter encounter2 = (PersonEncounter)arg0;
		Date date1 = encounter1.getDateStart();
		Date date2 = encounter2.getDateStart();
		int result = 0;
		if (date1 == null) {
			result = -1;
		} else if (date2 == null) {
			result = 1;
		} else {
			date1 = new Date(date1.getTime());
			date2 = new Date(date2.getTime());
			result = date1.compareTo(date2);
		}
		
		if (result == 0) {
			result = encounter1.getPerson().getId().compareTo(encounter2.getPerson().getId());
		}
		
		if (result == 0) {
			result = encounter1.getId().compareTo(encounter2.getId());
		}
		
		return result;
	}

}
